import { NestFactory, FastifyAdapter } from '@nestjs/core'
import {
  INestApplication,
  INestExpressApplication,
  HttpServer,
  INestMicroservice
} from '@nestjs/common'
import { from, Observable } from 'rxjs'
import { NestApplicationOptions } from '@nestjs/common/interfaces/nest-application-options.interface'
import { INestFastifyApplication } from '@nestjs/common/interfaces/nest-fastify-application.interface'

import { MicroserviceOptions } from '@nestjs/common/interfaces/microservices/microservice-configuration.interface'
import { NestMicroserviceOptions } from '@nestjs/common/interfaces/microservices/nest-microservice-options.interface'

export function createServer(
  module: any,
  options?: NestApplicationOptions
): Observable<INestApplication & INestExpressApplication> {
  return from(NestFactory.create(module, options))
}

export function createHttpServer(
  module: any,
  httpServer: HttpServer,
  options?: NestApplicationOptions
): Observable<INestApplication & INestExpressApplication> {
  return from(NestFactory.create(module, httpServer, options))
}

export function createFastifyServer(
  module: any,
  httpServer: FastifyAdapter,
  options?: NestApplicationOptions
): Observable<INestApplication & INestFastifyApplication> {
  return from(NestFactory.create(module, httpServer, options))
}

export function createMicroservice(
  module: any,
  options?: NestMicroserviceOptions & MicroserviceOptions
): Observable<INestMicroservice> {
  return from(NestFactory.createMicroservice(module, options))
}
